package com.nk.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nk.system.beans.DropdownBean;
import com.nk.system.beans.ProgramBean;
import com.nk.system.common.search.Search;
import com.nk.system.dao.ProgramDao;
import com.nk.system.domain.Program;
import com.nk.system.service.MasterService;

@Service("masterService")
public class MasterServiceImpl extends GenericServiceImpl<Program, String, ProgramDao>  implements MasterService{

	private Logger log = Logger.getLogger(MasterServiceImpl.class);

	@Autowired
	public MasterServiceImpl(ProgramDao programDao) {
		super(programDao);
	}



	@Override
	public List<DropdownBean> getProvinceList(String code,int type) {
		List<DropdownBean> dropdownList = new ArrayList<DropdownBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code != ""){
				search.addFilterEqual("zone", code);
			}
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("country");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program province:programList){
				if(!check.equals(province.getCountry())) {
					DropdownBean dropdownBean = new DropdownBean(); 
					dropdownBean.setValueCode(province.getCountry());
					dropdownBean.setTextName(province.getCountry());
					
					dropdownList.add(dropdownBean);
				}
				check = province.getCountry();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}



	@Override
	public List<DropdownBean> getUniversityList(String code, String province, int type) {
		// TODO Auto-generated method stub
		List<DropdownBean> dropdownList = new ArrayList<DropdownBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code != ""){
				search.addFilterEqual("zone", code);
			}
			if(province != null && province != ""){
				search.addFilterEqual("country", province);
			}
			
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("uname");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program uniName:programList){
				if(!check.equals(uniName.getUname())) {
					DropdownBean dropdownBean = new DropdownBean(); 
					dropdownBean.setValueCode(uniName.getUname());
					dropdownBean.setTextName(uniName.getUname());
					
					dropdownList.add(dropdownBean);
				}
				check = uniName.getUname();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}



	@Override
	public List<DropdownBean> getFacultyList(String code, String province, String university, int type) {
		// TODO Auto-generated method stub
		List<DropdownBean> dropdownList = new ArrayList<DropdownBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code != ""){
				search.addFilterEqual("zone", code);
			}
			if(province != null && province != ""){
				search.addFilterEqual("country", province);
			}
			if(university != null && university != ""){
				search.addFilterEqual("uname", university);
			}
			
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("faculty");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program faculty:programList){
				if(!check.equals(faculty.getFaculty())) {
					DropdownBean dropdownBean = new DropdownBean(); 
					dropdownBean.setValueCode(faculty.getFaculty());
					dropdownBean.setTextName(faculty.getFaculty());
					
					dropdownList.add(dropdownBean);
				}
				check = faculty.getFaculty();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}



	@Override
	public List<DropdownBean> getMajorList(String code, String province, String university, String ma, int type) {
		// TODO Auto-generated method stub
		List<DropdownBean> dropdownList = new ArrayList<DropdownBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code != ""){
				search.addFilterEqual("zone", code);
			}
			if(province != null && province != ""){
				search.addFilterEqual("country", province);
			}
			if(university != null && university != ""){
				search.addFilterEqual("uname", university);
			}
			if(ma != null && ma != ""){
				search.addFilterEqual("major", ma);
			}
			
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("major");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program major1:programList){
				if(!check.equals(major1.getMajor())) {
					if(major1.getMajor().equals("null")){}
					else{
						DropdownBean dropdownBean = new DropdownBean(); 
						dropdownBean.setValueCode(major1.getMajor());
						dropdownBean.setTextName(major1.getMajor());
					
						dropdownList.add(dropdownBean);
					}
				}
				check = major1.getMajor();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}
	
	@Override
	public List<DropdownBean> getProvinceList(List<String> code, int type) {
		// TODO Auto-generated method stub
		List<DropdownBean> dropdownList = new ArrayList<DropdownBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code.size() > 0){
				search.addFilterIn("zone", code);
			}
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("country");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program province:programList){
				if(!check.equals(province.getCountry())) {
					DropdownBean dropdownBean = new DropdownBean(); 
					dropdownBean.setValueCode(province.getCountry());
					dropdownBean.setTextName(province.getCountry());
					
					dropdownList.add(dropdownBean);
				}
				check = province.getCountry();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}



	@Override
	public List<DropdownBean> getUniversityList(List<String> code,
			List<String> province, int type) {
		// TODO Auto-generated method stub
		List<DropdownBean> dropdownList = new ArrayList<DropdownBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code.size() > 0){
				search.addFilterIn("zone", code);
			}
			if(province != null && province.size() > 0){
				search.addFilterIn("country", province);
			}
			
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("uname");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program uniName:programList){
				if(!check.equals(uniName.getUname())) {
					DropdownBean dropdownBean = new DropdownBean(); 
					dropdownBean.setValueCode(uniName.getUname());
					dropdownBean.setTextName(uniName.getUname());
					
					dropdownList.add(dropdownBean);
				}
				check = uniName.getUname();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}



	@Override
	public List<DropdownBean> getFacultyList(List<String> code,
			List<String> province, List<String> uname, int type) {
		// TODO Auto-generated method stub
		List<DropdownBean> dropdownList = new ArrayList<DropdownBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code.size() > 0){
				search.addFilterIn("zone", code);
			}
			if(province != null && province.size() > 0){
				search.addFilterIn("country", province);
			}
			if(uname != null && uname.size() > 0){
				search.addFilterIn("uname", uname);
			}
			
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("faculty");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program faculty:programList){
				if(!check.equals(faculty.getFaculty())) {
					DropdownBean dropdownBean = new DropdownBean(); 
					dropdownBean.setValueCode(faculty.getFaculty());
					dropdownBean.setTextName(faculty.getFaculty());
					
					dropdownList.add(dropdownBean);
				}
				check = faculty.getFaculty();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}



	@Override
	public List<DropdownBean> getMajorList(List<String> code,
			List<String> province, List<String> uname, List<String> faculty,
			int type) {
		// TODO Auto-generated method stub
		List<DropdownBean> dropdownList = new ArrayList<DropdownBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code.size() > 0){
				search.addFilterIn("zone", code);
			}
			if(province != null && province.size() > 0){
				search.addFilterIn("country", province);
			}
			if(uname != null && uname.size() > 0){
				search.addFilterIn("uname", uname);
			}
			if(faculty != null && faculty.size() > 0){
				search.addFilterIn("faculty", faculty);
			}
			
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("major");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program major1:programList){
				if(!check.equals(major1.getMajor())) {
					if(major1.getMajor().equals("null")){}
					else{
						DropdownBean dropdownBean = new DropdownBean(); 
						dropdownBean.setValueCode(major1.getMajor());
						dropdownBean.setTextName(major1.getMajor());
					
						dropdownList.add(dropdownBean);
					}
				}
				check = major1.getMajor();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}



	@Override
	public List<ProgramBean> getMajorList(List<String> code,
			List<String> province, List<String> uname, List<String> fac,
			List<String> major, int type) {
		// TODO Auto-generated method stub
		List<ProgramBean> dropdownList = new ArrayList<ProgramBean>();
		try {
			Search search = new Search(Program.class);
			if(code != null && code.size() > 0){
				search.addFilterIn("zone", code);
			}
			if(province != null && province.size() > 0){
				search.addFilterIn("country", province);
			}
			if(uname != null && uname.size() > 0){
				search.addFilterIn("uname", uname);
			}
			if(fac != null && fac.size() > 0){
				search.addFilterIn("faculty", fac);
			}
			if(major != null && major.size() > 0){
				search.addFilterIn("major", major);
			}
			
			if(type == 1){
				search.addFilterEqual("bachelor", 'T');
			}
			else if(type == 2){
				search.addFilterEqual("master", 'T');
			}
			else if(type == 3){
				search.addFilterEqual("doctor", 'T');
			}
			
			search.addSortAsc("major");

			List<Program> programList = dao.search(search);
			
			String check = "";
			for(Program major1:programList){
				if(!check.equals(major1.getMajor())) {
					if(major1.getMajor().equals("null")){}
					else{
						ProgramBean programBean = new ProgramBean(); 
						programBean.setId(major1.getId());
						//.....
					
						dropdownList.add(programBean);
					}
				}
				check = major1.getMajor();
				
			}
			
		}catch(Exception e) {
			log.error(e,e);
		}
		return dropdownList;
	}
	
	
}
